using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;

using GonzalesCore.CoreElements;

using Kypris2.Utils;
using Kypris2.CoreElements;
using Kypris2.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;
using Jamila2.Tools;

namespace GonzalesCore.CoreManagers
{
    public class ChangeManager : KyprisManagerBase, IManager, IDisposable
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();
        private PersonalSessionAccount CurrentModuleSession = null;
        private LogManager CurrentLogManager = null;

        public ChangeManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {
            this.CurrentModuleSession = PrivateConfig;
            this.CurrentLogManager = new LogManager(PrivateConfig);
        }

        #region CreateNewDetail

        #region createChangeClaimStatusDetail
        public bool createChangeClaimStatusDetail(ChangeStatus ChangeStatusDetail)
        {
            bool smooth = true;

            if (smooth)
            {
                DatabaseParameters ValParameters = new DatabaseParameters();

                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.InternalID.ActualFieldName, ChangeStatusDetail.InternalID));
                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ChangeRemark.ActualFieldName, ChangeStatusDetail.remark.Replace("'", "''"), true));
                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ChangeOriginalStatus.ActualFieldName, ClaimManager.GetClaimStatusInCode(ChangeStatusDetail.changeOriginStatus).ToString()));
                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ChangeToStatus.ActualFieldName, ClaimManager.GetClaimStatusInCode(ChangeStatusDetail.changeToStatus ).ToString()));
                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ClaimID.ActualFieldName, ChangeStatusDetail.ClaimID ));
                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ChangeDate.ActualFieldName, DateTime.Now));
                ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ChangeBy.ActualFieldName, ChangeStatusDetail.ChangeBy.InternalID));


                smooth = this.InsertChangeStatusDetail(ValParameters);
                if (!smooth)
                {
                    // log the error
                    if (this.CurrentLogManager.IsReady)
                    {
                        this.CurrentLogManager.LogError("[createChangeClaimStatusDetail:insertMaster_ChangeStatusDetail] : " + this.ErrMsg, this.CurrentModuleSession.CurrentLoggedOnUser, LogInterface.System);
                    }
                }
            }


            return smooth;
        }
         #endregion

        #region InsertChangeStatusDetail
        protected virtual bool InsertChangeStatusDetail(DatabaseParameters ValParameters)
        {
            bool smooth = true;

            if (this.TryConnection())
            {
                this.CurSQLFactory.InsertCommand(ValParameters, this.DataStructrure.Tables.ChangeClaimStatus.ActualTableName);
                smooth = this.CurDBEngine.ExecuteQuery(this.CurSQLFactory.SQL);
                if (!smooth)
                {
                    this.ErrMsg = this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                smooth = false;
            }

            return smooth;
        }
          #endregion

        #endregion

        #region getHistoryByClaimID

        public ChangeStatusItems GetDetailByClaimID(string ClaimID)
        {
            ChangeStatusItems ResultChangeStatus = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            DatabaseParameters SortParameters = new DatabaseParameters();
            DatabaseParameter SortByOrder = new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ChangeDate.ActualFieldName, "");
            SortByOrder.SortBy = true;
            SortByOrder.ASC = true;
            SortParameters.Add(SortByOrder);

            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.ChangeClaimStatus.ClaimID.ActualFieldName, ClaimID));

            if (this.TryConnection())
            {
                //this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructrure.Views.MasterChangeClaimStatus.ActualTableName);
               // DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);
                DataTable ResultTable = this.QueryData(KeyParameters, SortParameters, this.DataStructrure.Views.MasterChangeClaimStatus.ActualTableName);

                if (ResultTable != null)
                {
                    ResultChangeStatus = this.LoadStatuses(ResultTable);

                }
                else
                {
                    // log error
                    this.ErrMsg = "[ChangeManager.GetDetailByClaimID] : " + this.ErrMsg;
                }
            }
            else
            {
                this.ErrMsg = "[ChangeManager.GetDetailByClaimID] : " + this.ErrMsg;
            }

            return ResultChangeStatus;

        }
        #endregion

        #region LoadClaimHeaders
        protected ChangeStatusItems LoadStatuses(DataTable ResultTable)
        {
            ChangeStatusItems ResultHeaders = null;

            if (ResultTable != null)
            {
                ResultHeaders = new ChangeStatusItems();

                foreach (DataRow ResultRow in ResultTable.Rows)
                {
                    ResultHeaders.Add(this.LoadStatus(ResultRow));
                }
            }

            return ResultHeaders;
        }
        #endregion
        #region LoadClaimHeader
        protected ChangeStatus  LoadStatus(DataRow ResultRow)
        {
            ChangeStatus ResultHeader = null;

            ResultHeader = new ChangeStatus(
               ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.InternalID.ActualFieldName].ToString(),
               ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ChangeRemark.ActualFieldName].ToString());
            ResultHeader.ClaimID = ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ClaimID.ActualFieldName].ToString();
            ResultHeader.remark  = ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ChangeRemark.ActualFieldName].ToString();
            ResultHeader.ChangeBy  = new SystemUser(
                    ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ChangeBy.ActualFieldName].ToString(),
                    ResultRow[this.DataStructrure.Views.MasterChangeClaimStatus.DisplayName.ActualFieldName].ToString());
            ResultHeader.ChangeDate  = DateTime.Parse(ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ChangeDate.ActualFieldName].ToString());
            ResultHeader.changeOriginStatus = ClaimManager.GetClaimStatusFromCode(ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ChangeOriginalStatus.ActualFieldName].ToString());
            ResultHeader.changeToStatus  = ClaimManager.GetClaimStatusFromCode(ResultRow[this.DataStructrure.Tables.ChangeClaimStatus.ChangeToStatus.ActualFieldName].ToString());

            return ResultHeader;
        }
          #endregion


        #region GetChangeStatusHistory
        public DataTable GetChangeStatusHistory(string ClaimID)
        {
            DataTable ResultTable = null;

            using (ChangeManager CurManager = new ChangeManager(this.CurrentModuleSession))
            {
                ChangeStatusItems ResultObj = CurManager.GetDetailByClaimID(ClaimID);

                if (ResultObj != null)
                {
                    ResultTable = new DataTable();

                    ResultTable.Columns.Add(new DataColumn("internal_id", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("change_claimid", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("change_remark", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("change_originStatus", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("change_toStatus", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("change_date", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("change_by", Type.GetType("System.String")));
               

                    for (int x = 0; x < ResultObj.Count; x++)
                    {
                        DataRow ResultRow = ResultTable.NewRow();

                        ChangeStatus CurrentObj = (ChangeStatus)ResultObj[x];

                       // ResultRow["InternalID"] = CurrentObj.InternalID;
                        ResultRow["change_claimid"] = CurrentObj.ClaimID;
                        ResultRow["change_remark"] = CurrentObj.remark ;
                        ResultRow["change_originStatus"] = ClaimManager.GetClaimStatusInString(CurrentObj.changeOriginStatus);
                        ResultRow["change_toStatus"] = ClaimManager.GetClaimStatusInString(CurrentObj.changeToStatus );
                        ResultRow["change_date"] = SwissArmy.DateConvert(CurrentObj.ChangeDate , this.CurrentModuleSession.CurrentLoggedOnUser.PreferedDateFormat, "/");
                        ResultRow["change_by"] = CurrentObj.ChangeBy.DisplayName;
                                           

                        ResultTable.Rows.Add(ResultRow);
                    }

                }
                else
                {
                    this.ErrMsg = "[ChangeManager.GetChangeStatusHistory] : ChangeManager.GetChangeStatusHistory('" + ClaimID + "') : " + CurManager.ErrorMessage;
                }
            }

            return ResultTable;
        }
        #endregion

    }
}
